"""
Case Type   : Ubtree
Case Name   : 建表插入数据，创建ubtree索引指定pcr_备机重建_指定-b full
Create At   : 2025-10-11
Owner       : sungang14
Description :
    1、建表;
    2、建索引时指定PCR
    3、插入数据
    4、停备机，主机继续插入数据
    5、备机重建
    6、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
History     :
"""
import os
import time
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

logger = Logger()


class SlowSQLTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.std1_db_sh = CommonSH('Standby1DbUser')
        self.std2_db_sh = CommonSH('Standby2DbUser')
        self.node_pri = Node(node='PrimaryDbUser')
        status = self.comsh.get_db_cluster_status('status')
        self.assertTrue(status)
        self.table_name = 't_ubtree_0119'
        self.prepare_data_00 = f"drop table if exists {self.table_name} cascade;"
        self.prepare_data_01 = f"""create table {self.table_name} (id int, name varchar(20)) with (storage_type=ustore)
                                    partition by hash (id) (
                                        partition p0,
                                        partition p1
                                    );"""
        self.prepare_data_02 = f"create index i_ubtree_0119 on {self.table_name} using ubtree(id) with (index_type=pcr);" \
                               f"select relkind, reloptions from pg_class where relname like 'i_ubtree_0119%';"
        self.prepare_data_03 = f"insert into {self.table_name} select g, 'name_'||g from generate_series(1,1000) as g;"
        self.prepare_data_04 = f"insert into {self.table_name} select g, 'name_'||g from generate_series(1001,1100) as g;"
        self.param_name = 'most_available_sync'
        res = self.common.show_param(self.param_name)
        if res != 'on':
            res = self.comsh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f'{self.param_name}=on')
            logger.info(res)
            self.assertTrue(res, "执行gs_guc set失败，预期成功但未找到标志信息")
        res = self.comsh.restart_db_cluster()
        logger.info(res)
        self.assertTrue(res)

    def test_slowsql(self):
        text = '--step1:建表;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_00)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, data_cmd, '执行失败' + text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_01)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, data_cmd, '执行失败' + text)

        text = "--step2:建索引时指定PCR;expect:成功--"
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_02)
        logger.info(data_cmd)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, data_cmd, '执行失败' + text)

        text = '--step3:插入数据;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_03)
        logger.info(data_cmd)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, data_cmd, '执行失败' + text)

        text = '-----step4:停备机，主机继续插入数据;expect:成功-----'
        logger.info(text)
        res = self.std1_db_sh.stop_db_instance()
        logger.info(res)
        res = self.std2_db_sh.stop_db_instance()
        logger.info(res)
        logger.info('---查看备机状态---')
        status = self.comsh.get_db_cluster_status('detail')
        logger.info(status)
        self.assertIn('Degrade', status, '执行失败：' + text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_04)
        logger.info(data_cmd)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, data_cmd, '执行失败' + text)

        text = '-----step5:备机重建;expect:成功-----'
        logger.info(text)
        build_res1 = self.std1_db_sh.build_standby()
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, build_res1)
        build_res2 = self.std2_db_sh.build_standby()
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, build_res2)
        time.sleep(10)
        status = self.comsh.get_db_cluster_status('status')
        self.assertTrue(status)

    def tearDown(self):
        text = '--step6:清理环境;expect:成功--'
        logger.info(text)
        data_cmd = self.comsh.execut_db_sql(self.prepare_data_00)
        logger.info(data_cmd)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, data_cmd, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')